package com.cyy.daijia.rules.service.impl;

import com.cyy.daijia.model.form.rules.RewardRuleRequest;
import com.cyy.daijia.model.form.rules.RewardRuleRequestForm;
import com.cyy.daijia.model.vo.rules.RewardRuleResponse;
import com.cyy.daijia.model.vo.rules.RewardRuleResponseVo;
import com.cyy.daijia.rules.service.RewardRuleService;
import com.cyy.daijia.utils.DroolsHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class RewardRuleServiceImpl implements RewardRuleService {
    private static final String RULES_CUSTOMER_RULES_DRL = "rules/RewardRule.drl";

    @Override
    public RewardRuleResponseVo calculateOrderRewardFee(RewardRuleRequestForm rewardRuleRequestForm) {
        RewardRuleRequest rewardRuleRequest = new RewardRuleRequest();
        rewardRuleRequest.setOrderNum(rewardRuleRequestForm.getOrderNum());
        //利用规则引擎
        KieSession kieSession = DroolsHelper.loadForRule(RULES_CUSTOMER_RULES_DRL);
        RewardRuleResponse rewardRuleResponse = new RewardRuleResponse();
        //设置传入值
        kieSession.setGlobal("rewardRuleResponse",rewardRuleResponse);
        kieSession.insert(rewardRuleRequest);
        //启动
        kieSession.fireAllRules();
        //终止
        kieSession.dispose();
        RewardRuleResponseVo rewardRuleResponseVo=new RewardRuleResponseVo();
        rewardRuleResponseVo.setRewardAmount(rewardRuleResponse.getRewardAmount());
        return rewardRuleResponseVo;
    }
}
